<?php
/**
 * Abstract base class for specific classes of widgets  
 * @author Jacek Lukasiewicz
 *
 */
abstract class Widget {
	
	/**
	 * widget options array
	 * @var array
	 */
	protected $options = array();
        
        /**
	 * html control attribute array
	 * @var array
	 */
        protected $attributes=array();
	
	/**
	 * options in a text format to the initialization widget function
	 * @var string
	 */
	protected $optionsString = '';
	
	/**
	 * widget functions array
	 * @var array
	 */
	protected $methods = array();
	
	/**
	 * widget events handlers array
	 * @var array
	 */
	protected $events = array();
        
        /**
         * event chain prepend before implemeting the widget
         * @var type 
         */
        protected $eventChain=array();
        
        /**
         * widget enabled only on trigger
         * 
         */
        protected $onTrigger=array();
        
        protected $afterWidgetCode="";
        protected $beforeWidgetCode="";
	
	/**
	 * DOM widget id
	 * @var string
	 */
	protected $id;

	/**
	 * widget template file name 
	 * @var string
	 */
	protected $template;
        
        protected $objValidate=null;

	 
	public function __construct() {
		$this->id = uniqid('w');
	}
		
	public function __set($field, $value) {
			$this->$field = $value;
	}
        
        public function setAttribute($key,$value)
        {
            $this->attributes[$key]=$value;
        }
        
        public function &validate()
        {
            include_once(dirname(__FILE__)."/JuiValidation.php");
            $this->objValidate=new JuiValidation();
            return $this->objValidate;
        }
        public function beforeWidget($code)
        {
            $this->beforeWidgetCode=$code;
        }
        public function afterWidget($code)
        {
            $this->afterWidgetCode=$code;
        }
	public function setTrigger($element_id,$event_name)
        {
            $this->onTrigger[]=array("element_id"=>$element_id,"event_name"=>$event_name);
        }
        /**
         * 
         * @param type $event_name
         * @param type string with comma separated by params
         */
        public function setEventChain($event_name,$param)
        {
            $this->eventChain[]=array("event_name"=>$event_name,"param"=>$param);
        }
	/**
	 * set widget initialization option
	 * @param string $key
	 * @param string $value
	 */
	public function setOption($key, $value) {
		$this->options[$key] = $value;
	}
	
	/**
	 * Run widget method
	 * @param string $method
	 * @param string $args
	 */
	public function runMethod($method, $args='') {
		$this->methods[$method] = $args;
	}
	
	
	/**
	 * Add event handler
	 * @param string $event
	 * @param string $handler
	 */
	public function addEventHandler($event, $handler) {
		$this->events[$event] = $handler;
	}
	
	
	/**
	 * Render a widget
	 */
	public function render() {
		$this->buildOptions();
 		ob_start();
		include(dirname(__FILE__).'/tpl/'.$this->template); 
 		$out = ob_get_clean();
 		return $out;
	}
	
	public function __toString() {
		return $this->render();
	}
	
	public function id() {
		
		return $this->id;
	}
	
	/**
	 * Build options string to the initialization widget function
	 */
	protected function buildOptions() {
		if(!empty($this->options)) {
			foreach($this->options as $opt=>$val)
                        {
                            if(empty($opt)) continue;
                            if(empty($this->optionsString))
                            {
                                $this->optionsString .= "{$opt}: {$val}";
                            }
                            else
                            {
                                $this->optionsString .= ",{$opt}: {$val}";
                            }
                            
			}
		}
	}

}